namespace ImpEx.Engine
{
    internal class FileTools
    {
        internal static string PackPathFormat(string name)
        {
            string m = FormatSeparator(name, "\\", "/");
            if (m.StartsWith("/"))
                m = m.Substring(1);

            return m;
        }

        internal static string BackwardPath(string name)
        {
            return FormatSeparator(name, "/", "\\");
        }

        internal static string FormatSeparator(string name, string a, string b)
        {
            name = name.Replace(a, b);
            while (name.IndexOf(b + b) >= 0)
                name = name.Replace(b + b, b);

            return name;
        }

        internal static string FormatFileName(string name)
        {
            name = BackwardPath(name);
            if (name.StartsWith("\\"))
                name = name.Substring(1);

            if (name.EndsWith("\\"))
                name = name.Remove(name.Length - 1);

            return BackwardPath(name);
        }

        internal static string FormatFolderName(string name)
        {
            name = FormatSeparator(name, "/", "\\");
            if (!name.EndsWith("\\"))
                name += "\\";
            return name;
        }
        internal static string FormatRelativePath(string rootDir, string fileDir)
        {
            return BackwardPath((fileDir.Length > rootDir.Length)
                                    ? fileDir.Substring(rootDir.Length)
                                    : "\\");
        }        
    }
}